<?php


namespace app\service\model;


use app\service\Y;

class FreightModel extends BaseModel
{

    protected $table = 'freight';

    /**
     * 保存数据
     * @param $param
     * @param $id
     * @return bool|\think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function saveData($param, $id) {
        if ($id > 0) {
            if (self::where('distance', '=', $param['distance'])->where('id', '<>', $id)->find()) {
                return Y::error('该距离已设置费用');
            }

            $data = self::find($id);
            if (!$data) {
                return Y::error('参数错误');
            }

            $data->save($param);
        } else {
            if (self::where('distance', '=', $param['distance'])->find()) {
                return Y::error('该距离已设置费用');
            }

            self::create($param);
        }
        return Y::success([], '保存成功');
    }

    /**
     * 计算运费
     * @param $distance
     * @return mixed
     */
    public static function calcFreight($distance)
    {
        return self::where('distance', '<', $distance)->max('money');
    }
}